package com.example.workmode.Alarm;

import java.util.Calendar;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;

public class RegisterAlarm extends BroadcastReceiver {
	
	SharedPreferences pref;
	private int hour;
	private int minute;

	@Override
	public void onReceive(Context context, Intent intent) {
		
		 Bundle data = intent.getExtras();
		 if (data.get("msg").equals("regAgain")) {
			// initialize values
			pref = context.getSharedPreferences("MyPrefs", 0);
		    hour = pref.getInt("alarmHour",	9);
		    minute = pref.getInt("alarmMinute", 0);
			 
			// Register
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, 1);
			cal.set(Calendar.HOUR_OF_DAY, hour);
			cal.set(Calendar.MINUTE, minute);
			cal.set(Calendar.SECOND, 0);
			Intent i = new Intent(context, PromptUserTask.class);
			i.putExtra("msg", "planForToday");
			i.addCategory("D" + String.valueOf(cal.get(Calendar.YEAR)) + String.valueOf(cal.get(Calendar.MONTH) + 1) + String.valueOf(cal.get(Calendar.DATE)));
			PendingIntent pi = PendingIntent.getBroadcast(context, 1, i, PendingIntent.FLAG_ONE_SHOT);
			AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
			am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);

		 }
	}

}
